'
' Copyright (c) 2004-2005
' by Peter Donker ( info@bring2mind.net )
'
' Permission is hereby granted to use and/or modify this software for a single server. It is 
' NOT permitted to publish, distribute, sublicense, and/or sell copies of this software in its
' original or modified form. You are not allowed to remove this copyright notice.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 
' BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
' NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
' DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
' OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OF THE SOFTWARE.
'
Imports System
Imports System.Data
Imports Bring2mind.DNN.Modules.Guestbook.Data
Imports System.Xml
Imports DotNetNuke.Services.Search

Namespace Bring2mind.DNN.Modules.Guestbook.Business

 Public Class GuestbookController
  Implements Entities.Modules.ISearchable
  Implements Entities.Modules.IPortable
  Private Const MAX_DESCRIPTION_LENGTH As Integer = 100

#Region " Optional Interfaces: Search "
  Public Function GetSearchItems(ByVal ModInfo As DotNetNuke.Entities.Modules.ModuleInfo) As DotNetNuke.Services.Search.SearchItemInfoCollection Implements DotNetNuke.Entities.Modules.ISearchable.GetSearchItems

   Dim sic As New SearchItemInfoCollection

   Dim objEntries As New EntriesController
   Dim objEntrie As EntriesInfo
   For Each objEntrie In objEntries.GetEntriesByModules(ModInfo.ModuleID, False)

    Dim content As String = HttpUtility.HtmlDecode(objEntrie.Body)
    Dim strDescription As String = HtmlUtils.Shorten(content, MAX_DESCRIPTION_LENGTH, "...")
    Dim si As New SearchItemInfo(ModInfo.ModuleTitle, strDescription, -1, objEntrie.DateSubmitted, ModInfo.ModuleID, objEntrie.EntryId.ToString, content, "EntryId=" & objEntrie.EntryId.ToString)
    sic.Add(si)
   Next

   Return sic

  End Function
#End Region

#Region " Optional Interfaces: Import/Export "
  Public Function ExportModule(ByVal ModuleID As Integer) As String Implements Entities.Modules.IPortable.ExportModule

   Dim strXml As String = "<Guestbook>"

   Dim Settings As New ModuleSettings(ModuleID)
   strXml += "<Modulesettings>"
   strXml += "<Roles>" + Settings.Roles + "</Roles>"
   strXml += "<PageSize>" + Settings.PageSize.ToString + "</PageSize>"
   strXml += "<NotificationEmail>" + Settings.NotificationEmail + "</NotificationEmail>"
   strXml += "<DateFormat>" + Settings.DateFormat + "</DateFormat>"
   strXml += "<ApprovalRole>" + Settings.ApprovalRole.ToString + "</ApprovalRole>"
   strXml += "<AllowPictures>" + Settings.AllowPictures.ToString + "</AllowPictures>"
   strXml += "<AllowHtml>" + Settings.AllowHtml.ToString + "</AllowHtml>"
   strXml += "<MaxPictureSize>" + Settings.MaxPictureSize.ToString + "</MaxPictureSize>"
   strXml += "<ThumbnailSize>" + Settings.ThumbnailSize.ToString + "</ThumbnailSize>"
   strXml += "</Modulesettings>"

   Dim ec As New EntriesController
   Dim e As EntriesInfo
   strXml += "<Entries>"
   For Each e In ec.GetEntriesByModules(ModuleID, False)
    strXml += "<Entry>"
    strXml += "<Body>" + DotNetNuke.Common.Utilities.XmlUtils.XMLEncode(e.Body) + "</Body>"
    strXml += "<DateSubmitted>" + e.DateSubmitted.ToString("u") + "</DateSubmitted>"
    strXml += "<Email>" + e.Email + "</Email>"
    strXml += "<Picture>" + e.Picture + "</Picture>"
    strXml += "<UserName>" + e.UserName + "</UserName>"
    strXml += "</Entry>"
   Next
   strXml += "</Entries>"

   strXml += "</Guestbook>"

   Return strXml

  End Function

  Public Sub ImportModule(ByVal ModuleID As Integer, ByVal Content As String, ByVal Version As String, ByVal UserId As Integer) Implements Entities.Modules.IPortable.ImportModule

   Dim xmlSettings As XmlNode = GetContent(Content, "Guestbook")
   Dim Settings As New ModuleSettings(ModuleID)
   With Settings
    .Roles = xmlSettings.SelectSingleNode("Modulesettings/Roles").InnerText
    .PageSize = CType(xmlSettings.SelectSingleNode("Modulesettings/PageSize").InnerText, Integer)
    .NotificationEmail = xmlSettings.SelectSingleNode("Modulesettings/NotificationEmail").InnerText
    .DateFormat = xmlSettings.SelectSingleNode("Modulesettings/DateFormat").InnerText
    .ApprovalRole = CType(xmlSettings.SelectSingleNode("Modulesettings/ApprovalRole").InnerText, Integer)
    .AllowPictures = CType(xmlSettings.SelectSingleNode("Modulesettings/AllowPictures").InnerText, Boolean)
    .MaxPictureSize = CType(xmlSettings.SelectSingleNode("Modulesettings/MaxPictureSize").InnerText, Integer)
    .ThumbnailSize = CType(xmlSettings.SelectSingleNode("Modulesettings/ThumbnailSize").InnerText, Integer)
    .AllowHtml = CType(xmlSettings.SelectSingleNode("Modulesettings/AllowHtml").InnerText, Boolean)
    .SaveSettings(ModuleID)
   End With

   Dim objEntries As New EntriesController
   Dim objEntrie As EntriesInfo

   ' Add new content
   Dim xmlM As XmlNode
   For Each xmlM In xmlSettings.SelectNodes("Entries/Entry")
    objEntrie = New EntriesInfo
    With objEntrie
     .Body = xmlM.SelectSingleNode("Body").InnerXml
     .DateSubmitted = Convert.ToDateTime(xmlM.SelectSingleNode("DateSubmitted").InnerText)
     .Email = xmlM.SelectSingleNode("Email").InnerText
     .IsApproved = True
     .ModuleId = ModuleID
     .Picture = xmlM.SelectSingleNode("Picture").InnerText
     .UserName = xmlM.SelectSingleNode("UserName").InnerText
    End With
    objEntries.AddEntries(objEntrie)
   Next

  End Sub
#End Region

 End Class

End Namespace
